package com.scenic.rd_android.threerd.rxjava_retrofit;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.nova.scenic.projectlibs.util.debug.MyLog;
import com.scenic.rd_android.R;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class TestRetrofit extends AppCompatActivity {

    MyLog mylog = new MyLog("TestRetrofit");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_retrofit);


    }


    Action1 action1 = new Action1<TestJavaBeans.GetServerInfo>() {
        @Override
        public void call(TestJavaBeans.GetServerInfo getServerInfo) {
            mylog.info(getServerInfo);
        }
    };

    public void testConnect1(View view) {


        getGetServerInfoObservable().
                subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(action1)
        ;

    }

    public void testConnect2(View view) {


        getGetServerInfoObservable()
                .subscribeOn(Schedulers.io())
                .subscribe(action1)
        ;


    }

    public void testConnect3(View view) {
        try {
            getGetServerInfoObservable()

                    .subscribe(action1)
            ;
        } catch (Exception e) {
            Toast.makeText(this,e.toString(),Toast.LENGTH_LONG).show();
        }
    }


    private Observable<TestJavaBeans.GetServerInfo> getGetServerInfoObservable() {
        final Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://121.40.213.101:10000")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();


        APITest api = retrofit.create(APITest.class);
        return api.getServerInfo4("100000");
    }


}
